package cn.com.cabinet.manager.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cabinet.base.util.PageUtils;
import com.cabinet.easy.util.Page;
import com.cabinet.service.dto.CommoditiesDto;
import com.cabinet.service.dto.CommoditiesPageUtil;
import com.cabinet.web.entity.Order;

import cn.com.cabinet.dao.CommoditiesDao;
import cn.com.cabinet.dao.OrderDao;
import cn.com.cabinet.dao.UserDao;
import cn.com.cabinet.manager.dto.OrderDto;
import cn.com.cabinet.util.JsonUtil;
import cn.com.easy.dto.MessageDTO;
import cn.com.easy.dto.PageRequestParamDTO;
import cn.com.easy.utils.ResponseOutputUtils;

@Controller
@RequestMapping("/manager/order")
public class OrderController {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private CommoditiesDao commoditiesDao;
	
	@Autowired
	private UserDao userDao;
	
	@RequestMapping("/index")
	public String index(){
		return "/Order/index";
		
	}
	
	@RequestMapping("/list")
	public void getOrderList(PageRequestParamDTO paramDto,HttpServletRequest req, HttpServletResponse res) throws IOException{
		
		int thispage = (paramDto.getPageNumber()-1)*10;
		
		PrintWriter pw = res.getWriter(); 
		List<Order> oList = orderDao.getOrderListLimit(thispage,paramDto.getPageSize());
		
		List<OrderDto> oDtoList = new ArrayList<>();
		
		OrderDto odto;
		
		for (Order o : oList) {
			odto = new OrderDto();
			BeanUtils.copyProperties(o, odto);
			odto.setCoName(commoditiesDao.findOne(o.getCommoditiesId()).getCoName());
			odto.setuName(userDao.findOne(o.getUserId()).getuName());
			oDtoList.add(odto);
		}
		int total = orderDao.getOrderList().size();
		String sellRecordJson = JsonUtil.parseJson(oDtoList);
		String json = "{\"total\":" + total + ",\"rows\":" + sellRecordJson + "}";
		pw.print(json);
	}

}
